package MyList;

import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput;

import java.util.Arrays;

public class MyArrayList implements Ilist{
    public int[] elem;
    public int useSize;
    public static final int DOUBLE_SIZE = 10;
    public MyArrayList() {
        this.elem = new int[DOUBLE_SIZE];
    }
    public MyArrayList(int capacity) {
        this.elem = new int[capacity];
    }

    @Override
    public void display() {
        for (int i = 0;i < this.useSize;i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

    @Override
    public void add(int data) {
        checkCapacity();
        this.elem[this.useSize] = data;
        this.useSize++;

    }
    @Override
    public void add(int pos, int data) {
        try {
            checkPosOnAdd(pos);
        } catch (PosIllegilaty e) {
            e.printStackTrace();
        }
        checkCapacity();
        for (int i = this.useSize-1;i >= pos;i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos ] = data;
        this.useSize++;
    }
    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        } else {
            for (int i = 0; i < this.useSize; i++) {
                if (this.elem[i] == toFind) {
                    return true;
                }
            }
            return  false;
        }
    }
    public boolean isEmpty() {
        return this.useSize == 0;
    }
    private void  checkPosOnAdd(int pos) throws PosIllegilaty {
        if (pos < 0 || pos >this.useSize) {
            System.out.println("不合法!");
            throw new PosIllegilaty("插入异常!"+pos);
        }
    }

    private void  checkCapacity() {
        if (isFull()) {
            elem = Arrays.copyOf(elem,elem.length*2);
        }
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {

    }

    @Override
    public boolean isFull() {
        if (this.useSize == elem.length) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int indexOf(int toFind) {
        if (isEmpty()) {
            return -1;
        } else {
            for (int i = 0; i < this.useSize; i++) {
                if (this.elem[i] == toFind) {
                    return i;
                }
            }
            return  -1;
        }
    }

    @Override
    public int get(int pos) throws MyArraysList {
        checkPosOnGetAndSet(pos);
        if (isEmpty()) {
            throw new MyArraysList("获取指定元素下标时顺序表为空");
        }
        return elem[pos];
    }
    private void  checkPosOnGetAndSet(int pos) throws PosIllegilaty {
        if (pos < 0 || pos >= this.useSize) {
            System.out.println("不合法!");
            throw new PosIllegilaty("获取指定元素下标异常!"+pos);
        }
    }

    @Override
    public void set(int pos, int value) {
        checkPosOnGetAndSet(pos);
        this.elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int i = indexOf(toRemove);
        if (i == -1) {
            System.out.println("没有找到");
            return;
        }
        for (int j = i;j < this.useSize-1;j++) {
            this.elem[j] = this.elem[j+1];
        }
        this.useSize--;
    }
}
